<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. Solr 官网搜索引擎是指一个庞大的互联网资源数据库，如网页，新闻组，程序，图像等。它有助于在万维网上定位信息。用户可以通过以关键字或短语的形式将查询传递到搜索引擎中来搜索信息。 搜索引擎然后搜索其数据库并向用户返回相关链接。">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="分布式--Solr-站内搜索引擎">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Distributed/分布式--Solr-站内搜索引擎/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. Solr 官网搜索引擎是指一个庞大的互联网资源数据库，如网页，新闻组，程序，图像等。它有助于在万维网上定位信息。用户可以通过以关键字或短语的形式将查询传递到搜索引擎中来搜索信息。 搜索引擎然后搜索其数据库并向用户返回相关链接。">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-bfc77c1a0a6eb2c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-1bf0758b0eb0684b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-de065e2ac8a1ce74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-baf64c7182a7a672.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-11090c5fa80e8f46.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9140c0bcaab06706.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d5f21f1310bfa724.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-04c075de4b0bf8bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-4349595cdcc44b88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-6ebd79b2ec87b707.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-80151e0e98a93eee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d4d8dfbcde7b4d8e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-332ef8a8631249ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-df610331adfc4da9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0949e58f8785d54e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-3a8c54b46e79445f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5743d83a7f6ef62f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-621650108f28be31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-29873a20d120b5a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8efbd0af3b9ecd9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-2b27b53df2c0065f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-7cf65b8c7fd70c42.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-fa87dbf8bca26090.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-6632928bbb90154b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-f579dd9e0da5aeea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-692d412bb6078167.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-b958792f1089387d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-bf408e3a521f03e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d001d4c3566d7605.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-b7d77df7d27cc1d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-4b915e58b5982a66.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8bbe4eaac48903b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-82118f3027942d1f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-2802c44a0ca47ad4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:07:19.183Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="分布式--Solr-站内搜索引擎">
<meta name="twitter:description" content="####1. Solr 官网搜索引擎是指一个庞大的互联网资源数据库，如网页，新闻组，程序，图像等。它有助于在万维网上定位信息。用户可以通过以关键字或短语的形式将查询传递到搜索引擎中来搜索信息。 搜索引擎然后搜索其数据库并向用户返回相关链接。">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-bfc77c1a0a6eb2c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Distributed/分布式--Solr-站内搜索引擎/"/>





  <title>分布式--Solr-站内搜索引擎 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Distributed/分布式--Solr-站内搜索引擎/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">分布式--Solr-站内搜索引擎</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. <a href="https://lucene.apache.org/" target="_blank" rel="noopener">Solr 官网</a><br>搜索引擎是指一个庞大的互联网资源数据库，如网页，新闻组，程序，图像等。它有助于在万维网上定位信息。用户可以通过以关键字或短语的形式将查询传递到搜索引擎中来搜索信息。 搜索引擎然后搜索其数据库并向用户返回相关链接。<br><a id="more"></a></p>
<p>######1). 搜索引擎基本组件</p>
<ul>
<li>Web爬虫 - Web爬虫也称为蜘蛛或机器人。 它是一个收集网络信息的软件组件。</li>
<li>数据库 - Web上的所有信息都存储在数据库中。它们包含大量的Web资源。</li>
<li>搜索接口 - 此组件是用户和数据库之间的接口。它帮助用户搜索数据库。<br>######2). 搜索引擎工作过程<br>步骤    |标题或名称    |描述|<br>— | — | — |<br>1    |获取原始内容    |任何搜索应用程序的第一步是收集要进行搜索的目标内容。|<br>2    |构建文档    |从原始内容构建文档，让搜索应用程序可以很容易地理解和解释。|<br>3    |分析文档    |在索引开始之前，将对文档进行分析。|<br>4    |索引文档    |当文档被构建和分析后，下一步是对它们建立索引，以便可以基于特定键而不是文档的全部内容来检索该文档。索引类似于在书开始页或末尾处的目录索引，其中常见单词以其页码显示，使得这些单词可以快速跟踪，而不是搜索整本书。|<br>5    |用于搜索的用户接口    |当索引数据库就绪，应用程序可以执行搜索操作。 为了帮助用户进行搜索，应用必须提供用户接口，用户可以在其中输入文本并启动搜索过程|<br>6    |构建查询    |当用户做出搜索文本的请求，应用程序应该使用该文本来准备查询对象，然后可以使用该查询对象来查询索引数据库以获得相关的细节。|<br>7    |搜索查询    |使用查询对象，检查索引数据库以获取相关详细信息和内容文档。|<br>8    |渲染结果    |当收到所需的结果，应用程序应决定如何使用其用户界面向用户显示结果。|<br><img src="https://upload-images.jianshu.io/upload_images/3110861-bfc77c1a0a6eb2c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></li>
</ul>
<p>######3). 安装<br>I. <a href="http://www.apache.org/dyn/closer.lua/lucene/solr/7.3.0" target="_blank" rel="noopener">Solr 下载</a>并解压<br><img src="https://upload-images.jianshu.io/upload_images/3110861-1bf0758b0eb0684b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p>II. 启动<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr start</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-de065e2ac8a1ce74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>III. 验证<br>浏览器输入<code>http://localhost:8983/</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-baf64c7182a7a672.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<p>######4). Solr 体系结构<br><img src="https://upload-images.jianshu.io/upload_images/3110861-11090c5fa80e8f46.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"><br>Solr的主要构建块(组件)</p>
<ul>
<li>请求处理程序 - 发送到Apache Solr的请求由这些请求处理程序处理。请求可以是查询请求或索引更新请求。根据这些请示的要求来选择请求处理程序。为了将请求传递给Solr，通常将处理器映射到某个URI端点，并且它将为指定的请求提供服务。</li>
<li>搜索组件 - 搜索组件是Apache Solr中提供的搜索类型(功能)。它可能是拼写检查，查询，构面，命中突出显示等。这些搜索组件被注册为搜索处理程序。多个组件可以注册到搜索处理程序。</li>
<li>查询解析器 − Apache Solr查询解析器解析传递给Solr的查询，并验证查询的语法是否有错误。解析查询后，将它们转换为Lucene理解的格式。</li>
<li>响应写入器 - Apache Solr中的响应写入器是为用户查询生成格式化输出的组件。 Solr支持XML，JSON，CSV等响应格式。对每种类型的响应都有不同的响应写入。</li>
<li>分析器/分词器 - Lucene以令牌的形式识别数据。 Apache Solr分析内容，将其分成令牌，并将这些令牌传递给Lucene。 Apache Solr中的分析器检查字段的文本并生成令牌流。分词器将分析器准备的令牌流分解成令牌。</li>
<li>更新请求处理器 - 每当向Apache Solr发送更新请求时，请求都通过一组称为更新请求处理器的插件(签名，日志记录，索引)运行。这个处理器负责修改，例如删除字段，添加字段等。<br>######5). Solr 术语</li>
<li>实例 - 就像一个tomcat实例或一个jetty实例，这个术语指的是在JVM中运行的应用程序服务器。Solr主目录提供对每个这些Solr实例的引用，一个或多个核心可以配置在每个实例中运行。</li>
<li>核心(core) - 在应用程序中运行多个索引时，可以在每个实例中拥有多个核心，而不是每个核心的多个实例。</li>
<li>主目录(home) - 术语$SOLR_HOME是指主目录，其中包含有关内核及其索引，配置和依赖关系的所有信息。</li>
<li>碎片(Shard) - 在分布式环境中，数据在多个Solr实例之间进行分区，其中每个数据块可以称为碎片(Shard)。它包含整个索引的子集。<br>######6). Solr Cloud 术语</li>
<li>节点(Node) - 在Solr云中，Solr的每个单个实例都被视为一个节点。</li>
<li>集群 - Solr云环境中的所有节点组合在一起构成集群。</li>
<li>集合 - 集群具有称为集合的逻辑索引。</li>
<li>碎片 - 碎片是集合的一部分，它具有一个或多个索引副本。</li>
<li>副本 - 在Solr Core中，在节点中运行的分片副本称为副本。</li>
<li>领导者(Leader) - 它也是碎片的副本，它将Solr Cloud的请求分发给剩余的副本。</li>
<li>Zookeeper - 这是一个Apache项目，Solr Cloud用于集中配置和协调，管理集群和选择领导者。<br>######7). Solr中的主要配置文件如下 -</li>
<li>Solr.xml - 它是包含Solr Cloud相关信息，此文件是在$SOLR_HOME目录中。 为了加载核心，Solr引用这个文件，这有助于识别它们。</li>
<li>Solrconfig.xml − 此文件包含与请求处理和响应格式化相关的定义，核心特定配置，以及索引，配置，管理内存和提交。</li>
<li>Schema.xml − 此文件包含整个模式以及字段和字段类型。</li>
<li>Core.properties - 此文件包含特定于核心的配置。它被引用为核心发现，因为它包含核心的名称和数据目录的路径。它可以在任何目录中使用，会将此目录它视为核心目录。<br>######8). 基本命令<br>I. 后台启动<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr start</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-9140c0bcaab06706.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>II. 停止<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr stop -all</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-d5f21f1310bfa724.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>III. 前台启动<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr start -f</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-04c075de4b0bf8bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>IV. 另一个端口上启动Solr<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr start -p 8081</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-4349595cdcc44b88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"></p>
<p>V. 重新启动Solr<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr start -p 8081</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-6ebd79b2ec87b707.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10.png"></p>
<p>VI. Solr 状态<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr status</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-80151e0e98a93eee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图11.png"></p>
<p>VII. Solr 帮助<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr -help</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-d4d8dfbcde7b4d8e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图12.png"></p>
<p>VIII. 指定端口停止<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr stop -p 8081</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-332ef8a8631249ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图13.png"></p>
<p>####2. Solr 使用</p>
<p>######1). Solr核心(Core)<br>核心是Lucene索引的运行实例，包含使用它所需的所有Solr配置文件。我们需要创建一个Solr Core来执行索引和分析等操作。Solr应用程序可以包含一个或多个核心。 如果需要，Solr应用程序中的两个核心可以相互通信。<br><img src="https://upload-images.jianshu.io/upload_images/3110861-df610331adfc4da9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图14.png"><br>I. create 命令创建<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr create -c solr_sample</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-0949e58f8785d54e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图15.png"></p>
<p>II. create_core创建<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr create_core -c my_core</span><br></pre></td></tr></table></figure></p>
<p>参数：</p>
<ul>
<li>-c core_name    要创建的核心的名称</li>
<li>-p port_name    要创建的核心的端口</li>
<li>-d conf_dir    端口的配置目录</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-3a8c54b46e79445f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图16.png"><br>这时刷新<code>http://localhost:8983/solr/#/</code>后可看到存在两个核心了<br><img src="https://upload-images.jianshu.io/upload_images/3110861-5743d83a7f6ef62f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图17.png"></p>
<p>III. 删除核心<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solr delete -c my_core</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-621650108f28be31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图18.png"></p>
<p>IV. 控制台创建<br>Core Admin -&gt; Add Core<br><img src="https://upload-images.jianshu.io/upload_images/3110861-29873a20d120b5a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图19.png"></p>
<p>######2). 索引<br>索引是系统地排列文档或(其他实体)。索引使用户能够在文档中快速地查找信息。</p>
<ul>
<li>索引集合，解析和存储文档。</li>
<li>索引是为了在查找所需文档时提高搜索查询的速度和性能。</li>
</ul>
<p>在Apache Solr中，我们可以索引(添加，删除，修改)各种文档格式，如xml，csv，pdf等。可以通过几种方式向Solr索引添加数据。<br>I. Post命令添加文档<br>Solr在其bin/目录中有一个post命令(Windows中无法使用)。使用这个命令，可以在Apache Solr中索引各种格式的文件。</p>
<ul>
<li>将<code>\solr-7.3.0\example\exampledocs\</code>目录下的post.jar拷贝到<code>\solr-7.3.0\bin</code>中, 使用命令执行添加文档<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -Dtype=text/csv -Dc=solr_sample -jar post.jar C:\Users\Administrator\Desktop\sample.csv</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>成功提示：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-8efbd0af3b9ecd9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图20.png"><br><strong>sample.csv即<code>solr-7.3.0\example\exampledocs</code>下的book.csv</strong><br>II. 查询<br>进入控制台<code>http://localhost:8983/solr/#/</code>, 选择solr_sample核心，点击Query-&gt;Execute Query 在右侧会显示出数据<br><img src="https://upload-images.jianshu.io/upload_images/3110861-2b27b53df2c0065f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图21.png"><br>数据显示格式可以自已选择，在中间栏<code>wt</code>选项可选择<br><img src="https://upload-images.jianshu.io/upload_images/3110861-7cf65b8c7fd70c42.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图22.png"></p>
<p>III. Web界面添加文档</p>
<ul>
<li><p>数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">       &#123;</span><br><span class="line">	&quot;id&quot;: &quot;001&quot;,</span><br><span class="line">	&quot;name&quot;: &quot;Ram&quot;,</span><br><span class="line">	&quot;age&quot;: 53,</span><br><span class="line">	&quot;Designation&quot;: &quot;Manager&quot;,</span><br><span class="line">	&quot;Location&quot;: &quot;Hyderabad&quot;</span><br><span class="line">&#125;,</span><br><span class="line">&#123;</span><br><span class="line">	&quot;id&quot;: &quot;002&quot;,</span><br><span class="line">	&quot;name&quot;: &quot;Robert&quot;,</span><br><span class="line">	&quot;age&quot;: 43,</span><br><span class="line">	&quot;Designation&quot;: &quot;SR.Programmer&quot;,</span><br><span class="line">	&quot;Location&quot;: &quot;Chennai&quot;</span><br><span class="line">&#125;,</span><br><span class="line">&#123;</span><br><span class="line">	&quot;id&quot;: &quot;003&quot;,</span><br><span class="line">	&quot;name&quot;: &quot;Rahim&quot;,</span><br><span class="line">	&quot;age&quot;: 25,</span><br><span class="line">	&quot;Designation&quot;: &quot;JR.Programmer&quot;,</span><br><span class="line">	&quot;Location&quot;: &quot;Delhi&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>Documents-&gt;Document(s)-&gt;Submit Document.<br><strong>注：在添加文档内容时，如果json数据根节点是数组，请只填写数组中的内容即可，默认提交时系统会设置为JSON数组</strong><br><img src="https://upload-images.jianshu.io/upload_images/3110861-fa87dbf8bca26090.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图23.png"></p>
</li>
</ul>
<p>IV.  Java Api 添加文档</p>
<ul>
<li><p>导包: 将<code>\solr-7.3.0\dist\solrj-lib\</code>路径下的jar包全部引入工程，<code>\solr-7.3.0\dist\</code>目录下的solr-solrj-7.3.0.jar引入工程，将<code>\solr-7.3.0\example\resources\</code>路径下的log4j.properties复制到工程src目录下<br><img src="https://upload-images.jianshu.io/upload_images/3110861-6632928bbb90154b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图24.png"></p>
</li>
<li><p>代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Solr Java Api 测试</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class SolrTest &#123;</span><br><span class="line">	// Solr链接</span><br><span class="line">	public static final String url = &quot;http://localhost:8983/solr/solr_sample&quot;;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 添加Document</span><br><span class="line">	 * @throws IOException </span><br><span class="line">	 * @throws SolrServerException </span><br><span class="line">	 */</span><br><span class="line">	@Test</span><br><span class="line">	public void addDocTest() throws SolrServerException, IOException &#123;</span><br><span class="line">		// 创建Solr客户端</span><br><span class="line">		SolrClient solrClient = new HttpSolrClient.Builder(url).build();</span><br><span class="line">		// 准备Solr文档</span><br><span class="line">		SolrInputDocument document = new SolrInputDocument();</span><br><span class="line">		// 添加字段</span><br><span class="line">		document.addField(&quot;id&quot;, &quot;022&quot;);</span><br><span class="line">		document.addField(&quot;name&quot;, &quot;mazaiting&quot;);</span><br><span class="line">		document.addField(&quot;age&quot;, &quot;24&quot;);</span><br><span class="line">		document.addField(&quot;addr&quot;, &quot;中国科学院理化研究所&quot;);</span><br><span class="line">		</span><br><span class="line">		// 添加文档到Solr</span><br><span class="line">		solrClient.add(document);</span><br><span class="line">		</span><br><span class="line">		// 保存修改</span><br><span class="line">		solrClient.commit();</span><br><span class="line">		System.out.println(&quot;Documents added.&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>测试结果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-f579dd9e0da5aeea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图25.png"></p>
</li>
</ul>
<p>######3). 添加XML文档<br>I. 数据<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">&lt;add&gt; </span><br><span class="line">   &lt;doc&gt; </span><br><span class="line">      &lt;field name = &quot;id&quot;&gt;001&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;first name&quot;&gt;Rajiv&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;last name&quot;&gt;Reddy&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;phone&quot;&gt;9848022337&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;city&quot;&gt;Hyderabad&lt;/field&gt; </span><br><span class="line">   &lt;/doc&gt;  </span><br><span class="line">   &lt;doc&gt; </span><br><span class="line">      &lt;field name = &quot;id&quot;&gt;002&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;first name&quot;&gt;Siddarth&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;last name&quot;&gt;Battacharya&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;phone&quot;&gt;9848022338&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;city&quot;&gt;Kolkata&lt;/field&gt; </span><br><span class="line">   &lt;/doc&gt;  </span><br><span class="line">   &lt;doc&gt; </span><br><span class="line">      &lt;field name = &quot;id&quot;&gt;003&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;first name&quot;&gt;Rajesh&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;last name&quot;&gt;Khanna&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;phone&quot;&gt;9848022339&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;city&quot;&gt;Delhi&lt;/field&gt; </span><br><span class="line">   &lt;/doc&gt;  </span><br><span class="line">   &lt;doc&gt; </span><br><span class="line">      &lt;field name = &quot;id&quot;&gt;004&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;first name&quot;&gt;Preethi&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;last name&quot;&gt;Agarwal&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;phone&quot;&gt;9848022330&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;city&quot;&gt;Pune&lt;/field&gt; </span><br><span class="line">   &lt;/doc&gt;  </span><br><span class="line">   &lt;doc&gt; </span><br><span class="line">      &lt;field name = &quot;id&quot;&gt;005&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;first name&quot;&gt;Trupthi&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;last name&quot;&gt;Mohanthy&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;phone&quot;&gt;9848022336&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;city&quot;&gt;Bhuwaeshwar&lt;/field&gt; </span><br><span class="line">   &lt;/doc&gt; </span><br><span class="line">   &lt;doc&gt; </span><br><span class="line">      &lt;field name = &quot;id&quot;&gt;006&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;first name&quot;&gt;Archana&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;last name&quot;&gt;Mishra&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;phone&quot;&gt;9848022335&lt;/field&gt; </span><br><span class="line">      &lt;field name = &quot;city&quot;&gt;Chennai&lt;/field&gt; </span><br><span class="line">   &lt;/doc&gt; </span><br><span class="line">&lt;/add&gt;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>add − 这是用于将文档添加到索引的根标记。它包含一个或多个要添加的文档。</li>
<li>doc − 添加的文档应该包含在<doc> &lt;/ doc&gt;标记中。文档包含字段形式的数据。</doc></li>
<li>field − 字段标记包含文档的字段的名称和值。<br>II. Post命令添加<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -Dtype=text/xml -Dc=solr_sample -jar post.jar C:\Users\Administrator\Desktop\sample.xml</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-692d412bb6078167.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图26.png"></p>
<p>######4). 更新文档<br>I. 数据<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;add&gt;   </span><br><span class="line">   &lt;doc&gt;     </span><br><span class="line">      &lt;field name = &quot;id&quot;&gt;001&lt;/field&gt;     </span><br><span class="line">      &lt;field name = &quot;first name&quot; update = &quot;set&quot;&gt;Raj&lt;/field&gt;     </span><br><span class="line">      &lt;field name = &quot;last name&quot; update = &quot;add&quot;&gt;Malhotra&lt;/field&gt;     </span><br><span class="line">      &lt;field name = &quot;phone&quot; update = &quot;add&quot;&gt;9000000000&lt;/field&gt;    </span><br><span class="line">      &lt;field name = &quot;city&quot; update = &quot;add&quot;&gt;Delhi&lt;/field&gt;   </span><br><span class="line">   &lt;/doc&gt; </span><br><span class="line">&lt;/add&gt;</span><br></pre></td></tr></table></figure></p>
<p>唯一的区别是这里使用字段的一个update属性。<br>II. 命令<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -Dtype=text/xml -Dc=solr_sample -jar post.jar C:\Users\Administrator\Desktop\update.xml</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-b958792f1089387d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图27.png"></p>
<p>######5). 删除文档<br>I. 数据<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;delete&gt;   </span><br><span class="line">   &lt;id&gt;003&lt;/id&gt;   </span><br><span class="line">   &lt;id&gt;005&lt;/id&gt; </span><br><span class="line">&lt;/delete&gt;</span><br></pre></td></tr></table></figure></p>
<p>II. Post命令<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -Dtype=text/xml -Dc=solr_sample -jar post.jar C:\Users\Administrator\Desktop\delete.xml</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-bf408e3a521f03e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图28.png"></p>
<p>III. 删除字段<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;delete&gt; </span><br><span class="line">   &lt;query&gt;city:Chennai&lt;/query&gt; </span><br><span class="line">&lt;/delete&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-d001d4c3566d7605.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图29.png"><br>IV. 删除所有文档<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;delete&gt; </span><br><span class="line">   &lt;query&gt;*:*&lt;/query&gt; </span><br><span class="line">&lt;/delete&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-b7d77df7d27cc1d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图30.png"><br>V. Java Api<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Solr Java Api 测试</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class SolrTest &#123;</span><br><span class="line">	// Solr链接</span><br><span class="line">	public static final String url = &quot;http://localhost:8983/solr/solr_sample&quot;;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 删除所有文档</span><br><span class="line">	 * @throws IOException </span><br><span class="line">	 * @throws SolrServerException </span><br><span class="line">	 */</span><br><span class="line">	@Test</span><br><span class="line">	public void delAllDocTest() throws SolrServerException, IOException &#123;</span><br><span class="line">		// 创建客户端</span><br><span class="line">		SolrClient solrClient = new HttpSolrClient.Builder(url).build();</span><br><span class="line">		</span><br><span class="line">		// 从Solr中删除文档</span><br><span class="line">		solrClient.deleteByQuery(&quot;*&quot;);</span><br><span class="line">	</span><br><span class="line">		// 提交</span><br><span class="line">		solrClient.commit();</span><br><span class="line">		System.out.println(&quot;Documents deteled&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######6). 检索数据<br>I. 代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Solr Java Api 测试</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class SolrTest &#123;</span><br><span class="line">	// Solr链接</span><br><span class="line">	public static final String url = &quot;http://localhost:8983/solr/solr_sample&quot;;</span><br><span class="line">		</span><br><span class="line">	/**</span><br><span class="line">	 * 检索数据</span><br><span class="line">	 * @throws IOException </span><br><span class="line">	 * @throws SolrServerException </span><br><span class="line">	 */</span><br><span class="line">	@Test</span><br><span class="line">	public void retrieveDataTest() throws SolrServerException, IOException &#123;</span><br><span class="line">		// 创建客户端</span><br><span class="line">		SolrClient solrClient = new HttpSolrClient.Builder(url).build();</span><br><span class="line">		// 创建查询</span><br><span class="line">		SolrQuery query = new SolrQuery();</span><br><span class="line">		// 设置查询条件</span><br><span class="line">		query.setQuery(&quot;*:*&quot;);</span><br><span class="line">		// 添加查询字段</span><br><span class="line">		query.addField(&quot;*&quot;);</span><br><span class="line">		// 执行查询</span><br><span class="line">		QueryResponse queryResponse = solrClient.query(query);</span><br><span class="line">		// 获取查询到的结果集</span><br><span class="line">		SolrDocumentList results = queryResponse.getResults();</span><br><span class="line">		System.out.println(results);</span><br><span class="line">		System.out.println(results.get(0));</span><br><span class="line">		System.out.println(results.get(1));</span><br><span class="line">		System.out.println(results.get(2));</span><br><span class="line">		System.out.println(results.get(3));</span><br><span class="line">		System.out.println(results.get(4));</span><br><span class="line">		System.out.println(results.get(5));</span><br><span class="line">		// 提交</span><br><span class="line">		solrClient.commit();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 执行结果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-4b915e58b5982a66.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图31.png"></p>
<p>######7). 查询数据<br>I. 查询参数</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>q</td>
<td>这是Apache Solr的主要查询参数，文档根据它们与此参数中的术语的相似性来评分。</td>
<td></td>
</tr>
<tr>
<td>fq</td>
<td>这个参数表示Apache Solr的过滤器查询，将结果集限制为与此过滤器匹配的文档。</td>
<td></td>
</tr>
<tr>
<td>start</td>
<td>start参数表示页面的起始偏移量，此参数的默认值为0。</td>
<td></td>
</tr>
<tr>
<td>rows</td>
<td>这个参数表示每页要检索的文档的数量。此参数的默认值为10。</td>
<td></td>
</tr>
<tr>
<td>sort</td>
<td>这个参数指定由逗号分隔的字段列表，根据该列表对查询的结果进行排序。</td>
<td></td>
</tr>
<tr>
<td>fl</td>
<td>这个参数为结果集中的每个文档指定返回的字段列表。</td>
<td></td>
</tr>
<tr>
<td>wt</td>
<td>这个参数表示要查看响应结果的写入程序的类型。</td>
<td></td>
</tr>
<tr>
<td>fl</td>
<td>表示索引显示那些field(*表示所有field,如果想查询指定字段用逗号或空格隔开(如：Name,SKU,ShortDescription或Name SKU ShortDescription【注：字段是严格区分大小写的】)</td>
<td></td>
</tr>
<tr>
<td>q.op</td>
<td>表示q 中 查询语句的 各条件的逻辑操作 AND(与) OR(或)</td>
<td></td>
</tr>
<tr>
<td>hl</td>
<td>是否高亮 ,如hl=true</td>
<td></td>
</tr>
<tr>
<td>hl.fl</td>
<td>高亮field ,hl.fl=Name,SKU</td>
<td></td>
</tr>
<tr>
<td>hl.snippets</td>
<td>默认是1,这里设置为3个片段</td>
<td></td>
</tr>
<tr>
<td>hl.simple.pre</td>
<td>高亮前面的格式</td>
<td></td>
</tr>
<tr>
<td>hl.simple.post</td>
<td>高亮后面的格式</td>
<td></td>
</tr>
<tr>
<td>facet</td>
<td>是否启动统计</td>
<td></td>
</tr>
<tr>
<td>facet.field</td>
<td>统计field</td>
<td></td>
</tr>
</tbody>
</table>
<p><strong>特殊字符：</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">1. “:” 指定字段查指定值，如返回所有值*:*</span><br><span class="line">2. “?” 表示单个任意字符的通配</span><br><span class="line">3. “*” 表示多个任意字符的通配（不能在检索的项开始使用*或者?符号）</span><br><span class="line">4. “~” 表示模糊检索，如检索拼写类似于”roam”的项这样写：roam~将找到形如foam和roams的单词；roam~0.8，检索返回相似度在0.8以上的记录。</span><br><span class="line">5. 邻近检索，如检索相隔10个单词的”apache”和”jakarta”，”jakarta apache”~10</span><br><span class="line">6. “^” 控制相关度检索，如检索jakarta apache，同时希望去让”jakarta”的相关度更加好，那么在其后加上”^”符号和增量值，即jakarta^4 apache</span><br><span class="line">7. 布尔操作符AND、||</span><br><span class="line">8. 布尔操作符OR、&amp;&amp;</span><br><span class="line">9. 布尔操作符NOT、!、- （排除操作符不能单独与项使用构成查询）</span><br><span class="line">10. “+” 存在操作符，要求符号”+”后的项必须在文档相应的域中存在</span><br><span class="line">11. ( ) 用于构成子查询</span><br><span class="line">12. [] 包含范围检索，如检索某时间段记录，包含头尾，date:[200707 TO 200710]</span><br></pre></td></tr></table></figure></p>
<p>II. 示例<br><img src="https://upload-images.jianshu.io/upload_images/3110861-8bbe4eaac48903b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图32.png"><br>查询解释：q查询所有，start,rows从第二条开始到第六条，fl字段过滤，wt显示数据格式</p>
<p>######8). Apache Solr中可用的faceting类型 -</p>
<ul>
<li>查询faceting - 返回当前搜索结果中与给定查询匹配的文档数。</li>
<li>日期faceting - 它返回在特定日期范围内的文档数。</li>
</ul>
<p>构面或分组(faceting)命令被添加到任何正常的Solr查询请求，并且faceting计数在同一个查询响应中返回。<br><img src="https://upload-images.jianshu.io/upload_images/3110861-82118f3027942d1f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图33.png"><br>勾选facet后，会出现facet.query、facet.field、facet.prefix三个选项， rows设置为0.<br>示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Solr Java Api 测试</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class SolrTest &#123;</span><br><span class="line">	// Solr链接</span><br><span class="line">	public static final String url = &quot;http://localhost:8983/solr/solr_sample&quot;;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 构面或分组</span><br><span class="line">	 * @throws IOException </span><br><span class="line">	 * @throws SolrServerException </span><br><span class="line">	 */</span><br><span class="line">	@Test</span><br><span class="line">	public void facetTest() throws SolrServerException, IOException &#123;</span><br><span class="line">		// 创建客户端</span><br><span class="line">		SolrClient solrClient = new HttpSolrClient.Builder(url).build();</span><br><span class="line">		// 准备Solr文档</span><br><span class="line">		SolrInputDocument document = new SolrInputDocument();</span><br><span class="line">		// 创建查询</span><br><span class="line">		SolrQuery query = new SolrQuery();</span><br><span class="line">		// 设置查询条件</span><br><span class="line">		query.setQuery(&quot;*:*&quot;);</span><br><span class="line">		// 设置终止列</span><br><span class="line">		query.setRows(0);</span><br><span class="line">		// 设置构面</span><br><span class="line">		query.addFacetField(&quot;author&quot;);</span><br><span class="line">		// 创建查询请求</span><br><span class="line">		QueryRequest request = new QueryRequest(query);</span><br><span class="line">		// 创建查询相应</span><br><span class="line">		QueryResponse response = request.process(solrClient);</span><br><span class="line">		// 打印</span><br><span class="line">		System.out.println(response.getFacetFields());</span><br><span class="line">		// 获取构面列表</span><br><span class="line">		List&lt;FacetField&gt; facetFields = response.getFacetFields();</span><br><span class="line">		// 遍历</span><br><span class="line">		for (FacetField facetField : facetFields) &#123;</span><br><span class="line">			// 获取值</span><br><span class="line">			List&lt;Count&gt; values = facetField.getValues();</span><br><span class="line">			for (Count count : values) &#123;</span><br><span class="line">				System.out.println(count.getName() + &quot; : &quot; + count.getCount() </span><br><span class="line">				+ &quot;[drilldown qry: &quot; + count.getAsFilterQuery());</span><br><span class="line">			&#125;</span><br><span class="line">			System.out.println(&quot;Hello&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-2802c44a0ca47ad4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图34.png"></p>
<p>####3. Solr</p>
<p>######1).  Solr 与 Lucene 的区别</p>
<ul>
<li>Lucene是一个开发源代码的全文检索引擎工具包,它不是一个完整的全文检索引擎,Lucene 提供了完整的查询引擎和索引引擎,目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者以Lucene 为基础构建全文检索引擎;</li>
<li>Solr 的目标是打造一款企业级的搜索引擎系统,它是一个搜索引擎服务,可以独立运行,通过 Solr 可以非常快速的构建企业的搜索引擎,通过 Solr 也可以高效的完成站内搜索功能;<br>######2). Solr 文件夹结构</li>
<li>bin: solr 的运行脚本;</li>
<li>contrib: solr 的插件,用于增强 solr 的功能;</li>
<li>dist: 该目录包含 build 过程中产生的 war 和 jar 文件, 以及相关的依赖文件;</li>
<li>docs: 文档;</li>
<li>example<ul>
<li>example/solr: 包含了默认配置信息的 Solr 的 Core 目录;</li>
<li>example/multicore: 包含了在Solr的 multicore 中设置的多个 Core 目录;</li>
<li>example/webapps: 包含了 solr.war, 该 war 可作为solr的运行实例工程;</li>
</ul>
</li>
</ul>
<p>####<a href="https://gitee.com/mazaiting/distributed" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Distributed/分布式--Redis-内存数据库/" rel="next" title="分布式--Redis-内存数据库">
                <i class="fa fa-chevron-left"></i> 分布式--Redis-内存数据库
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Distributed/分布式--Spring-Boot-微服务框架/" rel="prev" title="分布式--Spring-Boot-微服务框架">
                分布式--Spring-Boot-微服务框架 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
